﻿using System;
using JStore.Core.Entities;
using JStore.Data.Daos;
using SpeadFrm.MvcPager;

namespace JStore.Tasks
{
    public class NewsTask
    {
        private readonly NewsDao _newsDao = new NewsDao();

        public void Add(string title, string content, int userId, string userName)
        {
            var info = new News
                           {
                               CreateDate = DateTime.Now,
                               CreateUserId = userId,
                               CreateUserName = userName,
                               NewsContent = content,
                               Title = title,
                           };
            _newsDao.Add(info);
        }

        public void Update(int id, string title, string content)
        {
            var info = _newsDao.GetById(id);
            if (info != null)
            {
                info.Title = title;
                if (content != null && info.NewsContent != content)
                    info.NewsContent = content;
                _newsDao.Update(info);
            }
        }

        public PagedList<News> GetPagedList(int pageIndex, int pageSize)
        {
            return _newsDao.GetPagedList(pageIndex, pageSize);
        }

        public News GetById(int id)
        {
            return _newsDao.GetById(id);
        }

        public void DeleteById(int id)
        {
            _newsDao.DeleteById(id);
        }
    }
}